from django.contrib import admin
from library.models import *
import xadmin
from django.utils.html import format_html
from xadmin import views

class BannerAdmin(object):
	list_display = ('imgurl', 'content_url', 'weight', 'add_time')
	ordering = ('weight', )

class BookAdmin(object):
	list_display = ('name', 'author', 'cover', 'publish_time', 'publish_house', 'collect_count', 'remain_count', 'category')
	search_field = ('name', 'author', 'publish_house')
	list_per_page = 20
	list_filter = ('category', )


class CategoryAdmin(object):
	list_display = ('name', 'level', 'show_index_show', 'top_level')
	search_fields = ('name', )

	def show_index_show(requests, self):
		color_code = 'red' if self.index_show=='1' else '#504d4d'
		text_code = '是' if self.index_show=='1' else '否'
		return format_html(
			'<span style="color: {}; font-weight:bold">{}</span>',
			color_code,
			text_code,
		)
	show_index_show.short_description = u'主页显示'

	# def show_top_level(requests, self):
	# 	color = 'darkgoldenrod' if self.top_level=='顶级' else ''
	# 	return format_html(
	# 		'<span style="color: {};">{}</span>',
	# 		color,
	# 		self.top_level.name,
	# 	)
	# show_top_level.short_description = u'上级分类'



xadmin.site.register(Banner, BannerAdmin)
xadmin.site.register(Book, BookAdmin)
xadmin.site.register(Category, CategoryAdmin)


class GlobalSetting(object):
	site_title = "图书管后台管理系统"    # 后台页头
	site_footer = "晋中学院"              # 后台页尾
	# menu_style = 'accordion'


xadmin.site.register(views.CommAdminView, GlobalSetting)
